using System;
using Events;
using Globals.States;
using PrimeTween;
using UnityEngine;
using UnityEngine.Serialization;

namespace Actions
{
    /// <summary>
    /// 缩圈
    /// </summary>
    public class Ring : MonoBehaviour
    {
        /// <summary>
        /// 缩小到最小时触发
        /// </summary>
        public event Action ShrunkToMinSize;
        
        public float CurrentScalePercent => transform.localScale.x / _maxScale;

        [SerializeField] private VoidEventChannel tutorialStartedEvent;
        [SerializeField] private VoidEventChannel gameStartedEvent;
        
        [SerializeField] private Shooter shooter;

        [SerializeField] private float shrinkSpeed = 2f;
        
        [SerializeField] private float expandOnEnemyDied = 1f;
        [SerializeField] private float shrinkOnHitByEnemy = 4f;
        [SerializeField] private float gameOverScale = 1f;

        private float _maxScale;


        private void Awake()
        {
            _maxScale = transform.localScale.x;
        }

        private void Start()
        {
            tutorialStartedEvent.Raised += TutorialStartedEvent_Raised;
            gameStartedEvent.Raised += GameStartedEvent_Raised;
            PlayerChaser.AnyTargetHitPlayer += PlayerChaser_AnyTargetHitPlayer;
            
            transform.localScale = Vector3.zero;
            enabled = false;
        }

        private void OnDestroy()
        {
            tutorialStartedEvent.Raised -= TutorialStartedEvent_Raised;
            gameStartedEvent.Raised -= GameStartedEvent_Raised;
            PlayerChaser.AnyTargetHitPlayer -= PlayerChaser_AnyTargetHitPlayer;
        }

        private void OnEnable()
        {
            shooter.AnyTargetDied += Shooter_AnyTargetDied;
        }

        private void Update()
        {
            ChangeScale(-shrinkSpeed * Time.deltaTime);
            CheckGameOver();
        }
        
        private void TutorialStartedEvent_Raised()
        {
            Tween.Scale(transform, _maxScale, 0.3f);
        }
        
        private void GameStartedEvent_Raised()
        {
            enabled = true;
        }
        
        private void PlayerChaser_AnyTargetHitPlayer()
        {
            if (GameState.Instance.CurrentState is TutorialState)
            {
                return;
            }
            
            ChangeScale(-shrinkOnHitByEnemy);
        }
        
        private void Shooter_AnyTargetDied()
        {
            Expand();
        }

        private void Expand()
        {
            ChangeScale(expandOnEnemyDied);
        }

        private void ChangeScale(float amount)
        {
            float scale = transform.localScale.x;
            scale += amount;
            if (scale > _maxScale)
            {
                scale = _maxScale;
            }
            if (scale < gameOverScale)
            {
                scale = gameOverScale;
            }
            
            transform.localScale = scale * Vector3.one;
        }

        private void CheckGameOver()
        {
            if (transform.localScale.x <= gameOverScale)
            {
                enabled = false;
                ShrunkToMinSize?.Invoke();
            }
        }
    }
}
